#!/usr/bin/env bash

function init() {
    master=$1
    if [[ "$master" != "0" ]]; then
        echo "Begin to initiate database..."
        for((x = 0; x <= ${master}-1; x++))
        do
           cat <<EOF | mysql -uapollo -pApollo2017!
drop database if exists master_${x};
create database master_${x}
  default char set utf8mb4
  default collate utf8mb4_general_ci;
drop database if exists master_${x}_slave;
create database master_${x}_slave
  default char set utf8mb4
  default collate utf8mb4_general_ci;
EOF
            if [[ -f /work/module/monica/schema/init_master_slave.sql ]]; then
                mysql -uapollo -pApollo2017! -D master_${x} < /work/module/monica/schema/init_master_slave.sql
                mysql -uapollo -pApollo2017! -D master_${x}_slave < /work/module/monica/schema/init_master_slave.sql
            fi
        done
    fi
}

# Clean databases
function clean() {
    master=$1
    if [[ "$master" != "0" ]]; then
        echo "Begin to delete database..."
        for((x = 0; x <= ${master}-1; x++))
        do
           cat <<EOF | mysql -uapollo -pApollo2017!
drop database if exists master_${x};
drop database if exists master_${x}_slave;
EOF
        done
    fi
}

init 4